<?php


		require_once('lib/ModelObject.php');

        define(USER_STATUS_DEFAULT, 1);
        define(USER_STATUS_ADMIN,   2);

        /*
         *        TODO (�������!!!)
         *        1. �������� ���� name
         *        2. �������� ���� family
         *        3. �������� ���� fname
         *        4. �������� ���� email
         *        5. �������� ���� telephone
         *        6. �������� ���� city
         */

        class User extends ModelObject {
                protected $modelName = 'User';
                protected $login = '';
                protected $password = '';
                protected $status = USER_STATUS_ADMIN;
                protected $name = '';
                protected $family = '';
                protected $fname = '';
                protected $email = '';
                protected $telephone = '';
                protected $city = '';

                function __construct($id=null) {
                        parent::__construct($id);
                }

                /**
                 * ��������� ������������. ���� ����� �����, �� ��������� �� ����, ���� ���� � �����������
                 * ������� ������ id. ���� ����� �� �����, �� ������ �� ��������� � ������������ id.
                 */
                public function Load($login='') {
                        if($login) {
                                $query = sprintf("SELECT `Id` FROM `User` WHERE `login`='%s'", mes($login));
                                $res = mysql_query($query);
                                if(!$res) throw new Exception("Unknown login");
                                $row = mysql_fetch_array($res);
                                if(!$row) throw new Exception("Unknown login");
                                $this->id = $row[0];
                        }

                        parent::Load();

                        $query = sprintf("SELECT
                                                `login`, `password`, `status`, `name`, `family`, `fname`, `email`, `telephone`, `city`
                                         FROM
                                             `User`
                                         WHERE
                                              `Id`=%d", $this->id);
                        $res = mysql_query($query);
                        $row = mysql_fetch_array($res);

                        $this->login = $row[0];
                        $this->password = $row[1];
                        $this->status = $row[2];
                        $this->name = $row[3];
                        $this->family = $row[4];
                        $this->fname = $row[5];
                        $this->email = $row[6];
                        $this->telephone = $row[7];
                        $this->city = $row[8];
                }

                /**
                 * ��������� ������� User
                 */
                public static function Install() {
                    ModelObject::Install();

                        if(ModelObject::TableIsExists('User')) return;

                        $query = sprintf("
                                        CREATE TABLE `User` (
                                          `Id`                        int(32) unsigned NOT NULL,
                                          `login`                varchar(40) NOT NULL,
                                          `password`        varchar(40) NOT NULL,
                                          `status`                int(4) NOT NULL DEFAULT '1',
                                          PRIMARY KEY  (`Id`),
                                          UNIQUE KEY (`login`),
                                          FOREIGN KEY (`Id`) REFERENCES `Object`(`id`) ON DELETE CASCADE
                                        ) ENGINE=INNODB DEFAULT CHARSET=utf8
                                ");
                        mysql_query($query);

                        $query=sprintf("ALTER TABLE `User` ADD `name` VARCHAR( 200 ) NOT NULL ,
                                              ADD `family` VARCHAR( 200 ),
                                              ADD `fname` VARCHAR( 200 ),
                                              ADD `email` VARCHAR( 150 ) NOT NULL ,
                                              ADD `telephone` VARCHAR( 50 ) NOT NULL ,
                                              ADD `city` VARCHAR( 200 )
									  ");
                        mysql_query($query);


                        $query = sprintf("INSERT INTO `ObjectType` SET `name` = '%s'", strval(__CLASS__));
                        mysql_query($query);

                        echo "User installed\n";
                }

                public function AsSource($otherName='User') {
                        $res = parent::AsSource($otherName);

                        $res->Attribute('login', $this->login);
						$res->Attribute('name', $this->name);
						$res->Attribute('family', $this->family);
						$res->Attribute('fname', $this->fname);
						$res->Attribute('email', $this->email);
						$res->Attribute('telephone', $this->telephone);
						$res->Attribute('city', $this->city);
                        $res->Attribute('status', $this->status);


						
                        return $res;
                }

                public function RegisterObject() {
                        parent::RegisterObject();
                        $query = sprintf("
                                        INSERT INTO `User` (
                                                        `Id`,
                                                        `login`,
                                                        `password`,
                                                        `status`,
                                                        `email`,
                                                        `telephone`,
                                                        `family`,
                                                        `name`,
                                                        `fname`,
                                                        `city`)
                                                VALUES (%d, '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s')",
                                $this->id, mes($this->login), mes($this->password), $this->status, mes($this->email), mes($this->telephone), mes($this->family), mes($this->name), mes($this->fname), mes($this->city));
                        $res = mysql_query($query);
                        if(mysql_errno()) {
                                $this->Remove();
                                throw new UserDuplicateException("Trying register exists user");
                        }
                }

                public function Save() {
                        parent::Save();
                        $query = sprintf("
                                        UPDATE `User`
                                        SET
                                                `login`='%s',
                                                `password`='%s',
                                                `status`=%d,
                                                `email`='%s',
                                                `telephone`='%s',
                                                `family`='%s',
                                                `name`='%s',
                                                `fname`='%s',
                                                `city`='%s'

                                        WHERE
                                                `Id`=%d",
                                        mes($this->login), mes($this->password), mes($this->status), mes($this->email), mes($this->telephone), mes($this->family), mes($this->name), mes($this->fname), mes($this->city), $this->id
                                );
                       	mysql_query($query);
                }

                public function Login($newLogin=false) {
                        if($newLogin)
                                $this->login = $newLogin;
                        return $this->login;
                }

                public function SetPassword($password) {
                        $this->password = sha1($password);
                }
				
				public function GetId() {
                        return $this->id;
                }

                public function CheckPassword($password) {
                        return sha1($password)==$this->password;
                }

                public function Status($newStatus=false) {
                        if($newStatus)
                                $this->status = $newStatus;
                        return $this->status;
                }

                 public function Name($newName=false) {
                        if($newName)
                                $this->name = $newName;
                        return $this->name;
                }

                public function Family($newFamily=false) {
                        if($newFamily)
                                $this->family = $newFamily;
                        return $this->family;
                }

                public function Fname($newFname=false) {
                        if($newFname)
                                $this->fname = $newFname;
                        return $this->fname;
                }

                public function Email($newEmail=false) {
                        if($newEmail)
                                $this->email = $newEmail;
                        return $this->email;
                }

                public function Telephone($newTelephone=false) {
                        if($newTelephone)
                                $this->telephone = $newTelephone;
                        return $this->telephone;
                }

                public function City($newCity=false) {
                        if($newCity)
                                $this->city = $newCity;
                        return $this->city;
                }
        }

        class UserException extends Exception {
        }

        class UserDuplicateException extends UserException {
        }
?>